<?php

namespace App\Admin\Controllers;

use App\Models\AppVersion;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use App\Services\OSS;
use Illuminate\Support\Facades\Log;

class UploadController extends AdminController
{

    /**
     * 上传文件
     */
    public function file_upload(Request $request)
    {
        $file = $request->file('files');
        if (!$file) {
            exit("请上传文件");
        }
        if ($file->isValid()) {
            $bucket = config('oss.BucketName');
            $ext = $file->getClientOriginalExtension();

            $dirtype=3;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
            switch($dirtype)
            {
                case 1: $attach_subdir = date('ymd'); break;
                case 2: $attach_subdir = date('ym'); break;
                case 3: $attach_subdir = 'ext_'.$ext; break;
                case 4: $attach_subdir = date('Ym'); break;
            }
            //获取上传图片的临时地址
            $tmppath = $file->getRealPath();
            //生成文件名
            $fileName = time().'_'.uniqid() . '.' . $ext;
            //拼接上传的文件夹路径(按照日期格式1810/17/xxxx.jpg)
            $pathName = 'files/'. $attach_subdir.'/'.$fileName;
            //上传文件到阿里云OSS
            $result = OSS::privateUpload($bucket, $pathName, $tmppath, ['ContentType' => $file->getClientMimeType()]);
            if (!$result) {
                return echojson('上传文件失败',500);
            }
            //获取上传文件的Url链接
            $Url = OSS::getPublicObjectURL($bucket, $pathName);
            // 返回状态给前端，Laravel框架会将数组转成JSON格式
            return echojson('上传成功',200,$Url);
        }else{
            return echojson('上传失败',402);
        }

    }

    /**
     * 上传图片
     * */
    public function img_upload(Request $request)
    {
        $file = $request->file('image');
        if (!$file) {
            exit("请上传图片");
        }
        if ($file->isValid()) {
            // 获取文件相关信息
            $allowed_extensions = ["png", "jpg", "gif","jpeg"];
            if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                return ['error' => 'You may only upload png, jpg or gif.'];
            }
//            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名
            $realPath = $file->getRealPath();   //临时文件的绝对路径
            $content = time().'_'.uniqid().'.'.$ext;
            $bucket = config('oss.BucketName');

            $dirtype=2;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
            switch($dirtype)
            {
                case 1: $attach_subdir = date('ymd'); break;
                case 2: $attach_subdir = date('ym'); break;
                case 3: $attach_subdir = 'ext_'.$ext; break;
                case 4: $attach_subdir = date('Ym'); break;
            }

            $object = "bookPic/" . $attach_subdir.'/'.$content;
            $result = OSS::publicUpload($bucket,$object,$realPath);
            if (!$result) {
                return echojson('上传图片失败',0);
            }
            $img_url = OSS::getPublicObjectURL($bucket,$object);
            if($img_url){
                return echojson('success',1,$img_url);
            }else{
                return echojson('图片路径获取失败',0);
            }
        }

    }
}
